var pageNo = 1;
var allPage = 10;
var pageSize = 3;
function changePage(page) {
	pageNo = page;
}
var cloumnId = $('#cloumnId').val();
var listTplFile = $('#listTplFile').val();
var pageTplFile = $('#pageTplFile').val();

/*
 * url
 */
var listUrl = getContextPath() + "/resources/front/tpl/" + listTplFile + ".tpl";
var pageUrl = getContextPath() + "/resources/front/tpl/" + pageTplFile + ".tpl";

$(document).ready(function() {
	// 引入juicer模板
	$("#loadTplList").load(listUrl, function() {
		loadList(cloumnId)
		$("#loadTplPage").load(pageUrl, function() {
			loadPage(cloumnId);
		});
	});
})

/*
 * 内容列表
 */
function loadList(cloumnId) {
	$.ajax({
		type : "POST",
		async : true,
		data : {
			link : cloumnId
		},
		dataType : "json",
		url : getContextPath() + "/content/columns",
		success : function(data) {
			var tpl = $('#listContent').html();
			var html = juicer(tpl, data);
			$("#list").html(html);
			$('#'+cloumnId).attr("style","background :#eeeeee");
			for (var i = 0; i < js("menu").length; i++) {
				js("menu")[i].onclick = function() {
					delBackgroundColor();
					this.style.background = "#eeeeee";
				}
			}
		},
		error : function() {
			alert("网络错误");
		}
	});
}

/*
 * 列表模板
 */
var current='';
function loadPage(cloumnId) {
	if (cloumnId != current) {
		pageNo = 1;
	}
	current = cloumnId;
	$.ajax({
		type : "POST",
		async : true,
		data : {
			link : cloumnId,
			pageNo : pageNo,
			pageSize : pageSize
		},
		dataType : "json",
		url : getContextPath() + "/content/contents",
		success : function(data) {
			console.log(data);
			var totalNum = parseInt(data.contentNum);
			allPage = Math.ceil(totalNum / pageSize);
			var tpl = $('#pageContent').html();
			juicer.register("formatPro", formatPro);
			var html = juicer(tpl, data);
			$("#page").html(html);
			callPageShowById('pagination', pageNo, allPage, loadPage,
					changePage, cloumnId);
		},
		error : function() {
			alert("网络错误");
		}
	});
}
/*
 * 详情页面
 */
function loadDetail(contentId) {
	$.ajax({
		type : "POST",
		async : true,
		data : {
			contentId : contentId
		},
		dataType : "json",
		url : getContextPath() + "/content/detail",
		success : function(data) {
			console.log(data);
			var tpl = $('#pageDetailTpl').html();
			var html = juicer(tpl, data);
			$("#list").html(html);
		},
		error : function() {
			alert("网络错误");
		}
	});
}

function js(id) {
	return $("#"+id+" p");
}
function delBackgroundColor() {
	for (var i = 0; i < js("menu").length; i++) {
		js("menu")[i].style.background = '';
	}
}
// 分页调用方式(pageElement:显示分页的ul容器ID,curPage:当前页号,allPage:总体页数,
// loadDataFunc:回调加载数据函数名称,changePageF:回调改变页号函数名称)
function callPageShowById(pageElement, curPage, allPage, loadDataFunc,
		changePageF, val) {
	if (allPage < 1) {
		allPage = 1;
	}
	var options = {
		bootstrapMajorVersion : 3,
		size : 'normal',// small, normal, large
		currentPage : curPage,
		numberOfPages : PAGE_NUM,
		totalPages : allPage,
		itemContainerClass : function(type, page, current) {
			return (page === current) ? "active" : "cursor-hand";
		},
		itemTexts : function(type, page, current) {
			switch (type) {
			case "first":
				// return "首 1页";
				return "首页";
			case "prev":
				return "上一页";
			case "next":
				return "下一页";
			case "last":
				// return "尾 " + allPage + "页";
				return "尾页";
			case "page":
				return page;
			}
		},
		tooltipTitles : function(type, page, current) {
			switch (type) {
			case "first":
				return "第一页";
			case "prev":
				return "上一页";
			case "next":
				return "下一页";
			case "last":
				return "最后一页";
			case "page":
				return "第 " + page + " 页";
			}
		},
		onPageClicked : function(e, originalEvent, type, page) {
			changePageF(page);
			loadDataFunc(val);
		}
	};

	$('#' + pageElement).bootstrapPaginator(options);
}

// 格式化长度
var getFormatDesc = function(data) {
	return cut_str(data, 20);
}

var formatPro = function(data) {
	return cut_str(data, 180);
}

// 字符串截取 + ..
function cut_str(str, len) {
	if (str == null || str == "") {
		return "";
	}
	var returnStr = "";
	var char_length = 0;
	for (var i = 0; i < str.length; i++) {
		var son_str = str.charAt(i);
		encodeURI(son_str).length > 2 ? char_length += 1 : char_length += 0.5;
		if (char_length >= len) {
			var sub_len = char_length == len ? i + 1 : i;
			returnStr = str.substr(0, sub_len);
			break;
		}
	}
	if (returnStr == "") {
		returnStr = str;
	} else {
		returnStr = returnStr + "..";
	}
	return returnStr;
}
